package com.hqd.ch03.v51.boot.context.properties.bind;

import com.hqd.ch03.v51.boot.context.properties.source.ConfigurationProperty;
import com.hqd.ch03.v51.boot.context.properties.source.ConfigurationPropertyName;

import java.util.function.Consumer;

public class BoundPropertiesTrackingBindHandler extends AbstractBindHandler {

    private final Consumer<ConfigurationProperty> consumer;

    public BoundPropertiesTrackingBindHandler(Consumer<ConfigurationProperty> consumer) {
        this.consumer = consumer;
    }

    @Override
    public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {
        if (context.getConfigurationProperty() != null && name.equals(context.getConfigurationProperty().getName())) {
            this.consumer.accept(context.getConfigurationProperty());
        }
        return super.onSuccess(name, target, context, result);
    }

}